﻿namespace KnownCMS.Helpers;

/// <summary>
/// AI自动识别内容帮助者类。
/// </summary>
public class AIHelper
{
    /// <summary>
    /// 取得或设置AI标签数据列表。
    /// </summary>
    public static Dictionary<string, string[]> Tags { get; set; }

    private static List<string> SensitiveWords { get; } = [];

    /// <summary>
    /// 加载敏感词数据库。
    /// </summary>
    /// <param name="fileName">数据库文件。</param>
    public static void LoadSensitiveWords(string fileName)
    {
        if (!File.Exists(fileName))
            return;

        var content = File.ReadAllText(fileName);
        if (string.IsNullOrWhiteSpace(content))
            return;

        var items = content.Split(Environment.NewLine);
        SensitiveWords.Clear();
        SensitiveWords.AddRange(items);
    }

    /// <summary>
    /// 根据内容自动识别类别。
    /// </summary>
    /// <param name="categories">类别列表。</param>
    /// <param name="info">内容表单信息。</param>
    /// <returns>类别。</returns>
    public static string GetPostCategory(List<CmCategory> categories, PostFormInfo info)
    {
        return string.Empty;
    }

    /// <summary>
    /// 根据内容自动识别标签。
    /// </summary>
    /// <param name="info">内容表单信息。</param>
    /// <returns>标签。</returns>
    public static string GetPostTags(PostFormInfo info)
    {
        var tags = new List<string>();
        foreach (var tag in Tags)
        {
            foreach (var key in tag.Value)
            {
                if (info.Title.Contains(key) || info.Content.Contains(key))
                {
                    if (!tags.Contains(tag.Key))
                        tags.Add(tag.Key);
                    continue;
                }
            }
        }
        if (tags.Count == 0)
            tags.Add("其他");
        return string.Join(",", tags);
    }

    /// <summary>
    /// 判断文本是否包含敏感词。
    /// </summary>
    /// <param name="text">检测文本。</param>
    /// <returns></returns>
    public static bool HasSensitiveWords(string text)
    {
        if (string.IsNullOrEmpty(text))
            return false;

        if (SensitiveWords == null || SensitiveWords.Count == 0)
            return false;

        foreach (var item in SensitiveWords)
        {
            if (text.Contains(item))
                return true;
        }
        return false;
    }
}